<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="图片标题" prop="imgTitle">
        <el-input
          v-model="queryParams.imgTitle"
          placeholder="请输入图片标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到期时间" prop="expireDate">
        <el-date-picker clearable
          v-model="queryParams.expireDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择到期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="display">
        <el-input
          v-model="queryParams.display"
          placeholder="请输入是否显示"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['carousel:carousel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carousel:carousel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carousel:carousel:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carouselList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
  <el-table-column type="selection" width="55" align="center" />
  <el-table-column label="id" align="center" prop="carouselId" />
  <el-table-column label="图片地址" align="center" prop="imgAddress" width="100">
    <template slot-scope="scope">
      <image-preview :src="scope.row.imgAddress" :width="50" :height="50"/>
    </template>
  </el-table-column>
  <el-table-column label="图片标题" align="center" prop="imgTitle" />
  <el-table-column label="开始时间" align="center" prop="startDate" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
    </template>
  </el-table-column>
  <el-table-column label="到期时间" align="center" prop="expireDate" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
    </template>
  </el-table-column>
  <el-table-column label="状态" align="center" prop="display">
  <template slot-scope="scope">
    <el-tag :type="scope.row.display === '启用' ? 'success' : 'info'">{{ scope.row.display }}</el-tag>
  </template>
</el-table-column>
<el-table-column
  label="序号"
  align="center"
  prop="serialNumber"
  sortable
  width="100"
  :sort-orders="['ascending', 'descending']"
>
  <template slot-scope="scope">
    {{ scope.row.serialNumber }}
  </template>
</el-table-column>
  <el-table-column label="描述" align="center" prop="describe" />
  <el-table-column label="创建人" align="center" prop="createUser" />
  <el-table-column label="备注" align="center" prop="remarks" />
  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
    <template slot-scope="scope">
      <el-button
        size="mini"
        type="text"
        icon="el-icon-edit"
        @click="handleUpdate(scope.row)"
        v-hasPermi="['carousel:carousel:edit']"
      >修改</el-button>
      <el-button
        size="mini"
        type="text"
        icon="el-icon-delete"
        @click="handleDelete(scope.row)"
        v-hasPermi="['carousel:carousel:remove']"
      >删除</el-button>
    </template>
  </el-table-column>
</el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!-- 添加或修改轮播图对话框 -->
<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
  <el-form ref="form" :model="form" :rules="rules" label-width="120px">
    <el-row :gutter="24">
      <!-- 左侧列 -->

        <el-form-item label="图片地址" prop="imgAddress">
          <image-upload v-model="form.imgAddress" style="width: 100%;"/>
        </el-form-item>
        <el-col :span="12">
      <el-form-item label="图片标题" prop="imgTitle">
        <el-select v-model="form.imgTitle" placeholder="请选择图片标题">
          <el-option
            v-for="item in imgTitleOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input v-model="form.describe" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="display">
          <el-radio-group v-model="form.display">
            <el-radio-button label="启用">启用</el-radio-button>
            <el-radio-button label="禁用">禁用</el-radio-button>
          </el-radio-group>
        </el-form-item>


      </el-col>
      <!-- 右侧列 -->
      <el-col :span="12">


        <el-form-item label="序号" prop="serialNumber">
          <el-input-number
            v-model="form.serialNumber"
            :min="1"
            :max="10"
            controls-position="right"
            style="width: 100%;"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="到期时间" prop="expireDate">
          <el-date-picker clearable
            v-model="form.expireDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到期时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitForm">确 定</el-button>
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { listCarousel, getCarousel, delCarousel, addCarousel, updateCarousel } from "@/api/siyun/carousel/carousel";

export default {
  name: "Carousel",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      carouselList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
    pageNum: 1,
   pageSize: 10,
   imgAddress: null,
   imgTitle: null,
   startDate: null,
    expireDate: null,
   display: null,
    serialNumber: null,
      describe: null,
      createUser: null,
      remarks: null,
      orderByColumn: '', // 排序列名
      isAsc: '' // 升序还是降序
    },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      displayOptions: [
  { value: '启用', label: '启用' },
  { value: '禁用', label: '禁用' }
],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询轮播图列表 */
/** 查询轮播图列表 */
getList() {
  this.loading = true;
  listCarousel(this.queryParams).then(response => {
    // 对数据进行排序
    this.carouselList = response.rows.sort((a, b) => a.serialNumber - b.serialNumber);
    this.total = response.total;
    this.loading = false;
  });
},
// 处理排序变化
handleSortChange(column) {
  this.queryParams.orderByColumn = column.prop;
  this.queryParams.isAsc = column.order === 'ascending' ? 'asc' : 'desc';
  this.getList();
},
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carouselId: null,
        imgAddress: null,
        imgTitle: null,
        startDate: null,
        expireDate: null,
        display: null,
        serialNumber: null,
        describe: null,
        createUser: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carouselId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加轮播图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carouselId = row.carouselId || this.ids
      getCarousel(carouselId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改轮播图";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carouselId != null) {
            updateCarousel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarousel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carouselIds = row.carouselId || this.ids;
      this.$modal.confirm('是否确认删除轮播图编号为"' + carouselIds + '"的数据项？').then(function() {
        return delCarousel(carouselIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('advertisement/carousel/export', {
        ...this.queryParams
      }, `carousel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
